package plugin;

import java.util.ArrayList;

import Phase2.Node;
/**
 * 
 * broadcast an message to all node in hyperweb and get response into an arrayList
 * Domain: any node with id from 1 to n-1 in hyperweb
 * @author djb0646
 * 
 * 
 */
public class BroadcastWithAck extends Broadcast implements Visitor
{
	
	@Override
	public Object getResponse(Node node, ArrayList<Object> result)
	{
		if(result == null)
		{
			ArrayList<Object> list = new ArrayList<Object>();
			list.add(node);
			return list;
		}
		if(!result.contains(node))
		{
			result.add(node);
		}
		return result;
	}

/*	/**
	 * @pre
	 * n is not null
	 * @post
	 * each avaliable node in hyperweb has resceive and response the message , message stored in payload
	 * @return
	 * ArrayList of responce of all nodes.
	 * 
	 */
/*	@SuppressWarnings("unchecked")
	@Override
	public Object visit(Node n, Object information)
	{
		assertNotNull(n);
		BroadcastAlgorithm algorithm = new BroadcastAlgorithm();
		List<Node> nextNodes;
		Object temp;
		OriginMessagePair<Integer, Object> newInfo;
		
		if(information instanceof OriginMessagePair)
		{
			newInfo = (OriginMessagePair) information;
			temp = n.acceptWithResponse(algorithm, ((OriginMessagePair) information).getOrigin());
		}
		else
		{
			newInfo = new OriginMessagePair<Integer, Object>(n.getId(), information);
			temp = n.acceptWithResponse(algorithm, n.getId());
		}
		
		if(temp instanceof ArrayList)
		{
			nextNodes = (ArrayList<Node>) temp;
		}
		else
		{
			nextNodes = new ArrayList<Node>();		
		}
		
		ArrayList<Object> result = new ArrayList<Object>();
		result.add(n.getId());
			

		for(Node node : nextNodes)
		{
			node.setPayload("message", newInfo.getMessage());
			result.addAll((ArrayList<Object>)  node.acceptWithResponse(this, newInfo));
			//System.out.println("case`2"+node.getId());
			
		}	
		
		return result;
	}
*/

}
